package com.study.lilhan.niuke.demo;

/**
 * @Author: lihan
 * @Date : 2021/12/14- 12
 * @DESCRIPTION: 参加LeetCode 5
 * @version: 1.0
 */
public class My17 {
    public static void main(String[] args) {

        String A = "aaaa";
       int result = getLongestPalindrome(A);
        System.out.println(result);
    }

    public static int getLongestPalindrome (String A) {
        char[] chars = A.toCharArray();
        int len  = chars.length;
        int maxLen = 1;
        boolean[][] dp = new boolean[len][len];
        for (int i = 0; i <len ; i++) {
            dp[i][i] = true;
        }
        //dp[i][j] = dp[i+1][j-1] && chars[i]= chars[j]
        for (int i = len -2; i >=0 ; i--) {
            for (int j = i+1; j < len ; j++) {
                if(chars[i] != chars[j])continue;
                if(i+1 == j){
                    dp[i][j] = true;
                    if(maxLen <2 ){
                        maxLen = 2;
                    }
                    continue;
                }
               if(dp[i+1][j-1]){
                    dp[i][j] = true;
                    if((j-i+1) >maxLen){
                        maxLen = j-i +1;
                    }
               }

            }
        }

        return maxLen;
    }

}
